<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\User;
use app\common\tool\Result;
use think\exception\ValidateException;
use app\common\model\Learn as LearnModel;
use app\common\model\CourseChapter;
use app\common\model\UserCourse;
use app\common\model\Cate;

class Learn extends Base
{

    /**
     * 学习中心列表
     */
    public function getList()
    {
        try {
            $param = $this->request->param();
            $list = LearnModel::getPageList(
                [],
                'id,title,icon,duration',
                $this->getPage(),
                $this->getLimit(),
                false,
            );
            foreach ($list['data'] as &$item) {
                $item->duration = gmdate("i分钟s秒", intval($item->duration));
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 课程章节详情
     */
    public function info()
    {
        try {
            $param = $this->request->param();
            $item = LearnModel::where('id', $param['id'])->field('id,title,icon,duration,content,video')->find();
            if (empty($item)) {
                return Result::fail('暂无详情');
            }
            $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
            $item->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $item->content));
            $item->content = str_replace('\\', '', $item->content);
            // $item->video = getVideo($item->id);
            return Result::success('获取成功！', $item);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
